/* Includes ------------------------------------------------------------------*/
#include "key.h"
#include "timer.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
u8 keyCount = 0;
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

void Key_Init(void)
{
    // GPIO_Init(KEY_PORT, KEY_PIN, GPIO_MODE_IN_PU_IT);
    // EXTI_SetExtIntSensitivity(EXTI_PORT_GPIOD, EXTI_SENSITIVITY_FALL_ONLY);

    GPIO_Init(KEY_PORT, KEY_PIN, GPIO_MODE_IN_PU_NO_IT);
}

u8 Key_DownScan(void)
{
    static u8 lock1 = 1, lock2 = 0, lock3 = 0;
    static TimeCalcStru *timer;
    if ((GPIO_ReadInputPin(KEY_PORT, KEY_PIN) == 0) && (lock1))
    {
        lock1 = 0;
        lock2 = 1;
    }

    if (lock2)
    {
        timer = PopTimeCalc();
        timer->count = 0;
        lock2 = 0;
        lock3 = 1;
    }
    if ((timer->count >= 20) && (lock3))
    {
        if (GPIO_ReadInputPin(KEY_PORT, KEY_PIN) == 0)
        {
            while (GPIO_ReadInputPin(KEY_PORT, KEY_PIN) == 0)
                ;
            keyCount++;
            if(keyCount >= 2)
                keyCount = 0;
            PutTimeCalc(timer);
            lock3 = 0;
            lock1 = 1;
            return 1;
        }
    }

    return 0;
}

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
